package com.shop.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.opensymphony.xwork2.Action;

public class FileAction implements Action{
	
	private String name;
	private File[] doc; //List<File> doc
	private String[] docFileName; //List<String>
	private String[] docContentType;
	
	private String contentType;
	private long fileSize;
	private String fileName;

	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String upload() throws Exception {
		/*System.out.println("desc:" + name);
		System.out.println("Size:" + doc.length());
		System.out.println("Name:" + doc.getName());
		System.out.println("FileName:" + docFileName);
		System.out.println("ContentType:" + docContentType);*/
		
		for (int i = 0; i < doc.length; i++) {
			
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(doc[i]));
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:/upload",docFileName[i])));
			
			byte[] buffer = new byte[512];
			int len = -1;
			while((len = bis.read(buffer)) != -1) {
				bos.write(buffer,0,len);
			}
			
			bos.flush();
			bos.close();
			bis.close();
		}
		return SUCCESS;
	}
	
	public String download() {
		return SUCCESS;
	}
	
	public InputStream getMyimage() throws FileNotFoundException {
		File file = new File("C:/upload","zzz.pdf");
		
		fileSize = file.length();
		contentType = "application/pdf";
		fileName = "my.pdf";
		
		return new FileInputStream(file);
	}

	//get set
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public File[] getDoc() {
		return doc;
	}

	public void setDoc(File[] doc) {
		this.doc = doc;
	}

	public String[] getDocFileName() {
		return docFileName;
	}

	public void setDocFileName(String[] docFileName) {
		this.docFileName = docFileName;
	}

	public String[] getDocContentType() {
		return docContentType;
	}

	public void setDocContentType(String[] docContentType) {
		this.docContentType = docContentType;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	
	

	
}
